import { ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DA_SERVICE_TOKEN, ITokenService } from "@delon/auth";
import { Router } from "@angular/router";

interface userPassword {
  r_password: string;
  password: string;
  c_password: string;
}

@Component({
  selector: 'app-sys-profile-security',
  templateUrl: './security.component.html',
  styleUrls: ['./security.component.less']
})
export class SysProfileSecurityComponent implements OnInit {
  constructor(
    private msgSrv: NzMessageService,
    private http: _HttpClient,
    private cdr: ChangeDetectorRef,
    @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
    private router: Router
  ) {}
  userPassword!: userPassword;
  ngOnInit(): void {
    this.userPassword = {
      r_password: '',
      password: '',
      c_password: ''
    };
    this.cdr.detectChanges();
  }

  save() {
    // this.msgSrv.success(JSON.stringify(this.user));
    this.http.put(`api/user_password_change`, this.userPassword).subscribe(res => {
      if (res.err_code == 0) {
        this.msgSrv.success(res.msg);
        this.tokenService.clear();
        this.router.navigateByUrl(this.tokenService.login_url!);
      } else {
        this.msgSrv.error(res.msg);
      }
    });
    return false;
  }
}
